package com.qf.dao.impl;

import com.qf.dao.TypeDao;
import com.qf.pojo.Goods;
import com.qf.pojo.Type;
import com.qf.service.TypeService;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Wy
 * @version V1.0
 * @Project javaWeb项目
 * @Package com.qf.dao.impl
 * @Description:
 * @Date 2022/5/23 21:56
 */
public class TypeDaoImpl implements TypeDao {
    //创建dbUtils工具
    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDateSource());

    @Override
    public List<Type> findAll() {
        String sql = "select * from tb_goods_type";
        try {
            List<Type> list = queryRunner.query(sql, new BeanListHandler<Type>(Type.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Goods> findGoodsAll() {
        String sql = "select a.*, b.name typeName from tb_goods a " +
                "LEFT join tb_goods_type b ON a.tid = b.id";
        try {
            List<Goods> list = queryRunner.query(sql, new BeanListHandler<Goods>(Goods.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
